package com.quanquan.user.controller;

import com.quanquan.user.dto.UserLevelDTO;
import com.quanquan.user.exception.UserLevelException;
import com.quanquan.user.request.UserLevelRequestBody;
import com.quanquan.user.service.UserLevelService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import web.response.ResultBody;

import java.util.List;

/**
 * 功能描述:会员等级管理
 *
 * @ClassName: UserLevelController
 * @Author: xiaoqianh丶
 * @Date: 2019-04-01 13:25
 * @Version: V1.0
 */
@Controller
@RequestMapping("/user_level")
public class UserLevelController {

    @Autowired
    private UserLevelService userLevelService;

    /**
     * 新增会员等级
     * @param requestBody
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultBody addLevel (@RequestBody UserLevelRequestBody requestBody){
        String msg = userLevelService.createUserLevel(requestBody);
       return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),msg);
    }


    /**
     * @param requestBody 跟新会员等级信息
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultBody updateLevel (@RequestBody UserLevelRequestBody requestBody){
        String msg = null;
        try {
            msg = userLevelService.updateUserLevel(requestBody);
        } catch (UserLevelException e) {
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),msg);
    }

    /**
     * 会员等级列表
     *
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public ResultBody levelList() {
        List<UserLevelDTO> userLevelDTOS = userLevelService.list();
        if (userLevelDTOS.size()<=0) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", userLevelDTOS);
    }

    /**
     * 会员等级列表
     *
     * @return
     */
    @PostMapping("/find_by_id")
    @ResponseBody
    public ResultBody findByUserLevelId(@RequestBody UserLevelRequestBody requestBody) {
        UserLevelDTO userLevelDTO = userLevelService.findByUserLevelId(requestBody.getUserLevelId());
        if (userLevelDTO == null) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "FAIL");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", userLevelDTO);
    }


    /**
     * 后台直接修改会员等级
     * @param requestBody
     * @return
     */
    @PostMapping("/update_management")
    @ResponseBody
    public ResultBody updateLevelManagement(UserLevelRequestBody requestBody){
        Integer msg = userLevelService.upgradeLevelanual(requestBody);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),msg);
    }
}
